package ModelLayer;
import java.util.ArrayList;

/**
 * The DVDCollections is a container for the Class DVD, it has an ArrayList and a private constructor
 * so that we can use a single instance of it.
 * 
 * @author Gruppe A 
 * @version 1.0
 */
public class DVDCollections
{
    // instance variables - replace the example below with your own
    private ArrayList<DVD> myDVDs;
    private static DVDCollections instance;

    /**
     * Constructor for objects of class DVDCollections
     */
    private DVDCollections()
    {
        // initialise instance variables
        myDVDs = new ArrayList<DVD>();
    }

    /**
     * Method for checking if an instance of the class allready exists, if it doesn't one is created.
     * @return DVDCollections instance.
     */
    public static DVDCollections getInstance()
    {
        if(instance == null)
        {
            instance = new DVDCollections();
        }
        return instance;
    }

    /**
     * A mutator method for adding objects of the type DVD to the ArrayList.
     * @param DVD dvd.
     */
    public void addDVD(DVD dvd)
    {
        myDVDs.add(dvd);
    }

    /**
     * An accessor method for finding specific objects in the ArrayList, it takes a String variable as a parameter. And 
     * returns a specific object of DVD
     * @param String title.
     * @return DVD specific dvd.
     */
    public DVD findDVD(long id)
    {

        int index = 0;
        boolean searching = true;
        while(index < myDVDs.size() && searching)
        {
            DVD file = myDVDs.get(index);
            if(file.getId()== id)
            {
                searching = false;         
            }
            else
            {
                index++;
            }
        }
        if(!searching)
        {
            return myDVDs.get(index);
        }
        else
        {
            return null;
        }
    }

    /**
     * A mutator method for reomving an object of DVD from the ArrayList.
     * @param DVD dvd.
     */
    public void removeDVD(DVD dvd)
    {
        myDVDs.remove(dvd);
    }

    /**
     * A mutator method for updating the attributes of a specific DVD object. Takes different parameters and substitutes
     * them for the original data. Thus updating the object.
     * @param DVD dvd
     * @param String title
     * @param String director
     * @param long id
     */
    public void updateDVD(DVD dvd, String title, String director, long id)
    {
        if(dvd != null)
        {
            if(title != null)
            {
                dvd.setTitle(title);
            }

            if(director != null)
            {
                dvd.setDirector(director);
            }

            if(id != 0)
            {
                dvd.setId(id);
            }

        }

    }

    /**
     * A method for listing all the dvds and their copies in the 
     * dvd collections, it grabs all the important information and the 
     * ads it to a new ArrayList.
     * @return ArrayList alldvds
     */

    public ArrayList<String> listAllDVDs()
    {
        ArrayList<String> alldvds = new ArrayList<String>();
        
             for(DVD d: myDVDs)
        {
            alldvds.add("Title: "+d.getTitle());
            alldvds.add("Director: "+d.getDirector());
            alldvds.add("ID: "+Long.toString(d.getId()));
            alldvds.add("\n");
            alldvds.addAll(d.listAllCopies());
            alldvds.add("\n");
            
            
        }
    
        return alldvds;
    }
}